import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.text.TextUtils
import com.zhiwang.xiaoqu.App

class TokenManager {

    companion object {

        lateinit var context: Application

        fun init(context: App) {
            this.context = context
        }

        @SuppressLint("ApplySharedPref")
        fun saveToken(it: String?) {
            val preferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
            preferences
                .edit()
                .putString("Token", it)
                .commit()
        }

        @SuppressLint("ApplySharedPref")
        fun clearToken() {
            //清除token
            context.getSharedPreferences("DATA", Context.MODE_PRIVATE).edit()
                .clear().commit()
        }

        fun token(): String {
            val preferences = context.getSharedPreferences("DATA", Context.MODE_PRIVATE)
            return preferences.getString("Token", "")!!
        }


        fun getDeviceId() = context.getSharedPreferences("DATA", Context.MODE_PRIVATE).getString("deviceId", "")


        fun isFirstTimeInstall(): Boolean {
            val preferences = context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE)
            return TextUtils.isEmpty(preferences.getString("KEY_EVER_INSTALLED", ""))
        }

        fun install() {
            val preferences = context.getSharedPreferences("Install_Data", Context.MODE_PRIVATE)
            preferences.edit()
                .putString("KEY_EVER_INSTALLED", "notfirstInstalled")
                .apply()
        }
    }
}